using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.DB.Orm
{
    /// <summary></summary>
    [Serializable]
    public class OrmOutput:ResultBase
    {
        #region prop
        //private string _ConnStr;

        //public string ConnStr
        //{
        //    get { return _ConnStr; }
        //    set { _ConnStr = value; }
        //}

        //private string _TblName;

        //public string TblName
        //{
        //    get { return _TblName; }
        //    set { _TblName = value; }
        //}

        //private string _PkField;

        //public string PK
        //{
        //    get { return _PkField; }
        //    set { _PkField = value; }
        //}

        //private Type _PkDataType;

        //public Type PkDataType
        //{
        //    get { return _PkDataType; }
        //    set { _PkDataType = value; }
        //}

        //private bool _IsIdentity;

        //public bool IsIdentity
        //{
        //    get { return _IsIdentity; }
        //    set { _IsIdentity = value; }
        //}

        //private string _BindableObjAssemblyFileName;

        //public string BindableObjAssemblyFileName
        //{
        //    get { return _BindableObjAssemblyFileName; }
        //    set { _BindableObjAssemblyFileName = value; }
        //}

        //private string _BindableObjTypeFullName;

        //public string BindableObjTypeFullName
        //{
        //    get { return _BindableObjTypeFullName; }
        //    set { _BindableObjTypeFullName = value; }
        //}

        private object _BindableObj;
        /// <summary></summary>
        public object BindableObject
        {
            get { return _BindableObj; }
            set { _BindableObj = value; }
        }
        #endregion
        /// <summary></summary>
        public OrmOutput():base(Guid.Empty,Guid.Empty)
        {
            //this._ConnStr = string.Empty;
            //this._TblName = string.Empty;
            //this._PkField = string.Empty;
            //this._PkDataType = typeof(int);
            //this._IsIdentity = false;
            //this._BindableObjAssemblyFileName = string.Empty;
            //this._BindableObjTypeFullName = typeof(string).FullName;
            this._BindableObj = string.Empty;
        }
        /// <summary></summary>
        public OrmOutput(Guid componentID, Guid jobTicketID, object bindableObj)
            : base(componentID, jobTicketID)
        {
            //this._ConnStr = connStr;
            //this._TblName = tblName;
            //this._PkField = pkField;
            //this._PkDataType = pkType;
            //this._IsIdentity = isIdentity;
            //this._BindableObjAssemblyFileName = assemblyFileName;
            //this._BindableObjTypeFullName = objTypeName;
            this._BindableObj = bindableObj;
        }
        /// <summary></summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            //XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnectionString", this.ConnStr);
            //XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TblName);
            //XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PkField", this.PK);
            //XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PkType", this.PkDataType.FullName);
            //XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IsIdentity", this.IsIdentity.ToString());
            //XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BindableObjectAssemblyName", this.BindableObjAssemblyFileName);
            //XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BindableObjectTypeName", this.BindableObject.GetType().FullName);

            return xNode;
        }
        /// <summary></summary>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                OrmOutput output = new OrmOutput();
                // output.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                //output.ConnStr = xNode.Attributes.GetNamedItem("ConnectionString").Value;
                //output.TblName = xNode.Attributes.GetNamedItem("TableName").Value;
                //output.PK = xNode.Attributes.GetNamedItem("PkField").Value;
                //output.PkDataType = Type.GetType(xNode.Attributes.GetNamedItem("PkType").Value);
                //output.IsIdentity = bool.Parse(xNode.Attributes.GetNamedItem("IsIdentity").Value);
                //output.BindableObjAssemblyFileName =
                //    xNode.Attributes.GetNamedItem("BindableObjectAssemblyName").Value;
                //string binFolderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                //string assemblyFilePath = Path.Combine(binFolderPath, output.BindableObjAssemblyFileName);
                //Assembly assem = Assembly.LoadFile(assemblyFilePath);
                //output.BindableObjTypeFullName = xNode.Attributes.GetNamedItem("BindableObjectTypeName").Value;
                //Type bindableObjType = assem.GetType(output.BindableObjTypeFullName);
                //output.BindableObject = Activator.CreateInstance(bindableObjType);

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}
